// Copyright 2012 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package dfa.axis.v1_17;

import com.google.api.ads.dfa.lib.client.DfaSession;
import com.google.api.ads.dfa.lib.factory.DfaServices;
import com.google.api.ads.dfa.axis.v1_17.CreativeBase;
import com.google.api.ads.dfa.axis.v1_17.CreativeRecordSet;
import com.google.api.ads.dfa.axis.v1_17.CreativeRemote;
import com.google.api.ads.dfa.axis.v1_17.CreativeSearchCriteria;

/**
 * This example retrieves available creatives for a given advertiser and
 * displays the name and ID. To create an advertiser, run CreateAdvertiser.java.
 * Results are limited to the first 10.
 *
 * Credentials and properties in {@code fromFile()} are pulled from the
 * "ads.properties" file. See README for more info.
 *
 * Tags: creative.getCreatives
 *
 * @author Joseph DiLallo
 */
public class GetCreatives {

  public static void main(String[] args) throws Exception {
    long advertiserId = Long.parseLong("INSERT_ADVERTISER_ID_HERE");

    // Initialize your session.
    DfaSession session = new DfaSession.Builder().fromFile().build();

    // Request the creative service from the service client factory.
    CreativeRemote creativeService = new DfaServices().get(session, CreativeRemote.class);

    // Set up creative search criteria structure.
    CreativeSearchCriteria creativeSearchCriteria = new CreativeSearchCriteria();
    creativeSearchCriteria.setPageSize(10);
    creativeSearchCriteria.setAdvertiserId(advertiserId);

    // Get creatives for the selected criteria.
    CreativeRecordSet creatives = creativeService.getCreatives(creativeSearchCriteria);

    // Display creative name and its ID.
    if (creatives.getRecords() != null) {
      for (CreativeBase result : creatives.getRecords()) {
        System.out.printf("Creative with name \"%s\" and ID \"%s\" was found.%n",
            result.getName(), result.getId());
      }
    } else {
      System.out.println("No creatives found for your criteria");
    }
  }
}
